package com.jingogo.controller;

import com.jingogo.common.Result;
import com.jingogo.entity.User;
import com.jingogo.service.WalletService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 钱包控制器
 *
 * @author jingogo
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/wallet")
@Tag(name = "钱包管理", description = "钱包相关接口")
@Validated
public class WalletController {

    @Autowired
    private WalletService walletService;

    @PostMapping("/recharge")
    @Operation(summary = "钱包充值", description = "用户钱包充值")
    @PreAuthorize("hasRole('USER')")
    public Result<String> recharge(@RequestParam @NotNull(message = "用户ID不能为空") Long userId,
                                 @RequestParam @DecimalMin(value = "0.01", message = "充值金额必须大于0") BigDecimal amount,
                                 @RequestParam @NotBlank(message = "支付方式不能为空") String payMethod) {
        walletService.recharge(userId, amount, payMethod);
        return Result.success("充值成功");
    }

    @PostMapping("/withdraw")
    @Operation(summary = "钱包提现", description = "用户钱包提现")
    @PreAuthorize("hasRole('USER')")
    public Result<String> withdraw(@RequestParam @NotNull(message = "用户ID不能为空") Long userId,
                                 @RequestParam @DecimalMin(value = "0.01", message = "提现金额必须大于0") BigDecimal amount,
                                 @RequestParam @NotBlank(message = "银行卡号不能为空") String bankCardNumber) {
        walletService.withdraw(userId, amount, bankCardNumber);
        return Result.success("提现申请已提交");
    }

    @PostMapping("/transfer")
    @Operation(summary = "钱包转账", description = "用户间钱包转账")
    @PreAuthorize("hasRole('USER')")
    public Result<String> transfer(@RequestParam @NotNull(message = "转出用户ID不能为空") Long fromUserId,
                                 @RequestParam @NotNull(message = "转入用户ID不能为空") Long toUserId,
                                 @RequestParam @DecimalMin(value = "0.01", message = "转账金额必须大于0") BigDecimal amount,
                                 @RequestParam(required = false) String remark) {
        walletService.transfer(fromUserId, toUserId, amount, remark);
        return Result.success("转账成功");
    }

    @GetMapping("/balance")
    @Operation(summary = "查询余额", description = "查询用户钱包余额")
    @PreAuthorize("hasRole('USER')")
    public Result<BigDecimal> getBalance(@RequestParam @NotNull(message = "用户ID不能为空") Long userId) {
        BigDecimal balance = walletService.getBalance(userId);
        return Result.success(balance);
    }

    @GetMapping("/transactionRecord")
    @Operation(summary = "交易记录", description = "查询用户钱包交易记录")
    @PreAuthorize("hasRole('USER')")
    public Result<?> getTransactionRecord(@RequestParam @NotNull(message = "用户ID不能为空") Long userId,
                                        @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(walletService.getTransactionRecord(userId, pageNum, pageSize));
    }
}